//
//  testUSB.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/30/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
#include <stdio.h>
#include <string>

using namespace std;

struct MySampleStruct {
    int first;
    int second;
};

int main_older(int argc, const char *argv[])
{
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    io_service_t device;

    /* set up a matching dictionary for the class */
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    if (matchingDict == NULL)
    {
        return -1; // fail
    }

    /* Now we have a dictionary, get an iterator.*/
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
    {
        return -1;
    }
    
    io_name_t devName;
    io_string_t pathName;
    

    /* iterate */
    while ((device = IOIteratorNext(iter)))
    {
        IORegistryEntryGetName(device, devName);
        string deviceName(devName);
        
        if (deviceName == "VendorSpecificDevice") {
            break;
        }
        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */
    }
    
    
    printf("Device's name = %s\n", devName);
    IORegistryEntryGetPath(device, kIOServicePlane, pathName);
    printf("Device's path in IOService plane = %s\n", pathName);
    IORegistryEntryGetPath(device, kIOUSBPlane, pathName);
    printf("Device's path in IOUSB plane = %s\n", pathName);
    
    
    io_connect_t dataPort;
    kern_return_t kernResult;
 
    kernResult = IOServiceOpen(device, mach_task_self(), 0, &dataPort);
    
    {
        
        
        MySampleStruct sampleStruct = {586, 8756}; //Random numbers.
        int sampleNumber = 15; //Random number.
        IOByteCount structSize = sizeof(MySampleStruct);
        kern_return_t kernResult;
        
        
    }
    
    
    
   IOObjectRelease(device);

   /* Done, release the iterator */
   IOObjectRelease(iter);
   return 0;
}





